package chamster;

import chamster.statemachine.HamsterState;
import chamster.statemachine.StateMachine;
/**
 * Hamster who's behaviour is determined by a kind of state machine
 * @author mark
 *
 * @param <HamsterType> The concrete type of the Hamster that is implemented
 */
public abstract class StateMachineHamster<HamsterType extends HistoryHamster> extends HistoryHamster {

	private HamsterState<HamsterType> currentState;
	private final StateMachine<HamsterType> stateMachine;
	
	public StateMachineHamster(StateMachine<HamsterType> stateMachine, HamsterState<HamsterType> startState, int historySize) {
		super(historySize);
		this.currentState = startState;
		this.stateMachine = stateMachine;
	}

	@Override
	public void life() throws Exception {
		while(super.chargeLeft(1) && this.currentState != null) {
			System.out.println("state " + this.currentState);
			this.currentState = this.stateMachine.run((HamsterType)this, this.currentState);
			if(super.canPickup()) {
				super.pickup();
			}
		}
		if(this.currentState == null) {
			System.out.println("No new state found!");
		}
		if(!super.chargeLeft(1)) {
			System.out.println("Accu empty");
		}
	}

}
